package com.bookocean.controller;

import com.bookocean.application.common.Result;
import com.bookocean.application.service.BadgeApplicationService;
import com.bookocean.controller.dto.BadgeDto;
import com.bookocean.domain.utils.UserContextHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 徽章控制器
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/badge")
@RequiredArgsConstructor
public class BadgeController {

    private final BadgeApplicationService badgeApplicationService;

    /**
     * 获取所有徽章定义
     * 
     * @return 徽章定义列表
     */
    @GetMapping("/all")
    public Result<List<BadgeDto>> getAllBadges() {
        log.info("获取所有徽章定义");
        
        List<BadgeDto> badges = badgeApplicationService.getAllBadges();
        return Result.success(badges);
    }

    /**
     * 根据分类获取徽章定义
     * 
     * @param category 徽章分类
     * @return 徽章定义列表
     */
    @GetMapping("/category/{category}")
    public Result<List<BadgeDto>> getBadgesByCategory(@PathVariable String category) {
        log.info("根据分类获取徽章定义，category: {}", category);
        
        List<BadgeDto> badges = badgeApplicationService.getBadgesByCategory(category);
        return Result.success(badges);
    }

    /**
     * 获取当前用户的徽章列表
     * 
     * @return 用户徽章列表
     */
    @GetMapping("/my-badges")
    public Result<List<BadgeDto>> getMyBadges() {
        Long currentUserId = UserContextHolder.getUserId();
        log.info("获取当前用户徽章列表，userId: {}", currentUserId);
        
        List<BadgeDto> badges = badgeApplicationService.getUserBadges(currentUserId);
        return Result.success(badges);
    }

    /**
     * 获取指定用户的徽章列表
     * 
     * @param userId 用户ID
     * @return 用户徽章列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<BadgeDto>> getUserBadges(@PathVariable Long userId) {
        log.info("获取指定用户徽章列表，userId: {}", userId);
        
        List<BadgeDto> badges = badgeApplicationService.getUserBadges(userId);
        return Result.success(badges);
    }

    /**
     * 获取徽章详情
     * 
     * @param badgeId 徽章ID
     * @return 徽章详情
     */
    @GetMapping("/{badgeId}")
    public Result<BadgeDto> getBadgeDetail(@PathVariable Long badgeId) {
        Long currentUserId = UserContextHolder.getUserId();
        log.info("获取徽章详情，badgeId: {}, userId: {}", badgeId, currentUserId);
        
        BadgeDto badge = badgeApplicationService.getBadgeDetail(badgeId, currentUserId);
        return Result.success(badge);
    }

    /**
     * 设置徽章显示状态
     * 
     * @param badgeId 徽章ID
     * @param isVisible 是否显示
     * @return 设置结果
     */
    @PutMapping("/{badgeId}/visibility")
    public Result<Boolean> setBadgeVisibility(@PathVariable Long badgeId, @RequestParam Boolean isVisible) {
        Long currentUserId = UserContextHolder.getUserId();
        log.info("设置徽章显示状态，badgeId: {}, isVisible: {}, userId: {}", badgeId, isVisible, currentUserId);
        
        boolean result = badgeApplicationService.setBadgeVisibility(currentUserId, badgeId, isVisible);
        return Result.success(result);
    }

    /**
     * 获取用户徽章数量统计
     * 
     * @return 徽章数量
     */
    @GetMapping("/count")
    public Result<Integer> getMyBadgeCount() {
        Long currentUserId = UserContextHolder.getUserId();
        log.info("获取用户徽章数量统计，userId: {}", currentUserId);
        
        Integer count = badgeApplicationService.getUserBadgeCount(currentUserId);
        return Result.success(count);
    }

    /**
     * 获取指定用户的徽章数量统计
     * 
     * @param userId 用户ID
     * @return 徽章数量
     */
    @GetMapping("/count/{userId}")
    public Result<Integer> getUserBadgeCount(@PathVariable Long userId) {
        log.info("获取指定用户徽章数量统计，userId: {}", userId);
        
        Integer count = badgeApplicationService.getUserBadgeCount(userId);
        return Result.success(count);
    }

}
